/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.aries.ui.widget.demo.slice;

import com.aries.ui.widget.BasisDialog;
import com.aries.ui.widget.action.sheet.UIActionSheetDialog;
import com.aries.ui.widget.demo.ResourceTable;
import com.aries.ui.widget.demo.base.BaseAbilitySlice;
import com.aries.ui.widget.demo.util.NavigationBarControlUtil;
import com.aries.ui.widget.demo.util.SizeUtil;
import com.aries.ui.widget.i.DialogInterface;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.*;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.ToastDialog;

public class ActionSheetAbilitySlice extends BaseAbilitySlice implements Component.ClickedListener, DialogInterface.OnDismissListener {

    //系统方法无法单独获取到状态栏高度，只能获取到状态栏高度与底部导航栏高度之和，所以这里给一个固定高度
    private static final int STATUSBAR_DEFAULT_HEIGHT = 72;

    private boolean isDefaultItemColor = true;
    private boolean isShowTitle = true;
    private boolean isBackDim = true;
    private boolean isNavigation = true;
    private boolean isNavigationPlus = true;
    private boolean isShowMargin = true;
    private boolean isDefaultTitleColor = true;
    private boolean isDefaultCancelColor = true;
    private UIActionSheetDialog mDialog;
    private Switch switch1;
    private Switch switch2;
    private Switch switch3;
    private Switch switch4;
    private Switch switch5;
    private Switch switch6;
    private Switch switch7;
    private Switch switch8;
    private Text textView1;
    private Text textView2;
    private Text textView3;
    private Text textView4;
    private Text textView5;
    private Text textView6;
    private Text textView7;
    private Text textView8;

    @Override
    protected void setTitleBar() {
        titleBar.setTitleMainText("UIActionSheetDialog示例").setStatusBackgroundColor(RgbPalette.WHITE.asArgbInt());
    }

    @Override
    protected void initView(Intent intent) {
        initView();
    }

    @Override
    protected int getLayout() {
        return ResourceTable.Layout_ability_action_sheet;
    }
//
//    @Override
//    public void onStart(Intent intent) {
//        super.onStart(intent);
//        setUIContent(ResourceTable.Layout_ability_action_sheet);
//        initView();
//    }

    private void initView() {


        Text text1 = (Text) findComponentById(ResourceTable.Id_rtv_showActionSheet);
        Text text2 = (Text) findComponentById(ResourceTable.Id_rtv_showIOSActionSheet);
        Text text3 = (Text) findComponentById(ResourceTable.Id_rtv_showWeiXinActionSheet);
        Text text4 = (Text) findComponentById(ResourceTable.Id_rtv_showIconActionSheet);
        Text text5 = (Text) findComponentById(ResourceTable.Id_rtv_showGridActionSheet);
        Text text6 = (Text) findComponentById(ResourceTable.Id_rtv_showFragmentActionSheet);


        DependentLayout button1 = (DependentLayout) findComponentById(ResourceTable.Id_sBtn_titleActionSheet);
        DependentLayout button2 = (DependentLayout) findComponentById(ResourceTable.Id_sBtn_titleColorActionSheet);
        DependentLayout button3 = (DependentLayout) findComponentById(ResourceTable.Id_sBtn_itemColorActionSheet);
        DependentLayout button4 = (DependentLayout) findComponentById(ResourceTable.Id_sBtn_cancelColorActionSheet);
        DependentLayout button5 = (DependentLayout) findComponentById(ResourceTable.Id_sBtn_marginActionSheet);
        DependentLayout button6 = (DependentLayout) findComponentById(ResourceTable.Id_sBtn_backActionSheet);
        DependentLayout button7 = (DependentLayout) findComponentById(ResourceTable.Id_sBtn_navigationActionSheet);
        DependentLayout button8 = (DependentLayout) findComponentById(ResourceTable.Id_sBtn_navigationPlusActionSheet);


        switch1 = (Switch) findComponentById(ResourceTable.Id_iv_show_title);
        switch2 = (Switch) findComponentById(ResourceTable.Id_iv_custome_text_color);
        switch3 = (Switch) findComponentById(ResourceTable.Id_iv_coustom_item_color);
        switch4 = (Switch) findComponentById(ResourceTable.Id_iv_coustom_cancel_color);
        switch5 = (Switch) findComponentById(ResourceTable.Id_iv_cancelAndItem_no_gap);
        switch6 = (Switch) findComponentById(ResourceTable.Id_iv_background_tran);
        switch7 = (Switch) findComponentById(ResourceTable.Id_iv_not_control_bottom);
        switch8 = (Switch) findComponentById(ResourceTable.Id_iv_bottom_not_zhanwei);


        switch1.setClickedListener(this);
        switch2.setClickedListener(this);
        switch3.setClickedListener(this);
        switch4.setClickedListener(this);
        switch5.setClickedListener(this);
        switch6.setClickedListener(this);
        switch7.setClickedListener(this);
        switch8.setClickedListener(this);


        textView1 = (Text) findComponentById(ResourceTable.Id_show_title);
        textView2 = (Text) findComponentById(ResourceTable.Id_custome_text_color);
        textView3 = (Text) findComponentById(ResourceTable.Id_coustom_item_color);
        textView4 = (Text) findComponentById(ResourceTable.Id_coustom_cancel_color);
        textView5 = (Text) findComponentById(ResourceTable.Id_cancelAndItem_no_gap);
        textView6 = (Text) findComponentById(ResourceTable.Id_background_tran);
        textView7 = (Text) findComponentById(ResourceTable.Id_not_control_bottom);
        textView8 = (Text) findComponentById(ResourceTable.Id_bottom_not_zhanwei);

        button1.setClickedListener(this);
        button2.setClickedListener(this);
        button3.setClickedListener(this);
        button4.setClickedListener(this);
        button5.setClickedListener(this);
        button6.setClickedListener(this);
        button7.setClickedListener(this);
        button8.setClickedListener(this);


        text1.setClickedListener(this);
        text2.setClickedListener(this);
        text3.setClickedListener(this);
        text4.setClickedListener(this);
        text5.setClickedListener(this);
        text6.setClickedListener(this);


        switch1.setChecked(true);
        switch2.setChecked(true);
        switch3.setChecked(true);
        switch4.setChecked(true);
        switch5.setChecked(true);
        switch6.setChecked(true);
        switch7.setChecked(true);
        switch8.setChecked(true);

        initCheckLisenter();

    }

    private void initCheckLisenter() {

        switch8.setCheckedStateChangedListener((absButton, isChecked) -> {
            isNavigationPlus = isChecked;
            textView8.setText(isNavigationPlus ? "底部虚拟导航栏增加View占位" : "底部虚拟导航栏不增加View占位");
        });


        switch7.setCheckedStateChangedListener((absButton, isChecked) -> {
            isNavigation = isChecked;
            textView7.setText(isNavigation ? "控制底部虚拟导航栏" : "不控制底部虚拟导航栏");
        });


        switch6.setCheckedStateChangedListener((absButton, isChecked) -> {
            isBackDim = isChecked;
            textView6.setText(isBackDim ? "背景半透明" : "背景全透明");
        });

        switch5.setCheckedStateChangedListener((absButton, isChecked) -> {
            isShowMargin = isChecked;
            textView5.setText(isShowMargin ? "cancel按钮与item之间有间隙" : "cancel按钮与item之间无间隙");
        });

        switch4.setCheckedStateChangedListener((absButton, isChecked) -> {
            isDefaultCancelColor = isChecked;
            textView4.setText(isDefaultCancelColor ? "默认Cancel文本颜色" : "自定义Cancel文本颜色");
        });

        switch3.setCheckedStateChangedListener((absButton, isChecked) -> {
            isDefaultItemColor = isChecked;
            textView3.setText(isDefaultItemColor ? "默认Item文本颜色" : "自定义Item文本颜色");
        });

        switch2.setCheckedStateChangedListener((absButton, isChecked) -> {
            isDefaultTitleColor = isChecked;
            textView2.setText(isDefaultTitleColor ? "默认Title文本颜色" : "自定义Title文本颜色");
        });

        switch1.setCheckedStateChangedListener((absButton, isChecked) -> {
            isShowTitle = isChecked;
            textView1.setText(isShowTitle ? "显示Title" : "隐藏Title");
        });

    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_rtv_showActionSheet:
                new UIActionSheetDialog.ListSheetBuilder(this)
                        .addItems(ResourceTable.Strarray_arrays_items_action)
                        .setItemsTextColorResource(ComponentState.COMPONENT_STATE_EMPTY, isDefaultItemColor ? ResourceTable.Color_colorActionSheetNormalItemText : ResourceTable.Color_holo_purple)
                        .setTitle(isShowTitle ? "哈哈" : null)
                        .setCancel(ResourceTable.String_cancel)
                        .setItemsMinHeight(200)
                        .setNavigationBarControl(isNavigation ? NavigationBarControlUtil.getNavigationBarControl(isNavigation, isNavigationPlus) : null)
                        .setCancelMarginTop(SizeUtil.dp2px(isShowMargin ? 8 : 0))
                        .setCancelTextColorResource(ComponentState.COMPONENT_STATE_EMPTY, isDefaultCancelColor ? ResourceTable.Color_colorActionSheetNormalItemText : ResourceTable.Color_darker_gray)
                        .setOnItemClickListener(mOnItemClickListener)
                        .create()
                        .setDimAmount(isBackDim ? 0.6f : 0f)
                        .show();
                break;
            case ResourceTable.Id_rtv_showIOSActionSheet:
                new UIActionSheetDialog.ListIOSBuilder(this)
//                        .setNavigationBarControl(isNavigation ? NavigationBarControlUtil.getNavigationBarControl(isNavigation, isNavigationPlus) : null)
//                        .addItem(Html.fromHtml(String.format(mFormat, mFilePath)))
                        .addItems(ResourceTable.Strarray_arrays_items_action)
                        .setItemsTextColorResource(ComponentState.COMPONENT_STATE_EMPTY, isDefaultItemColor ? ResourceTable.Color_colorActionSheetItemText : ResourceTable.Color_holo_purple)
                        .setTitle(isShowTitle ? "标题" : null)
                        .setCancel(ResourceTable.String_cancel)
                        .setCancelMarginTop(SizeUtil.dp2px(isShowMargin ? 8 : 0))
                        .setCancelTextColorResource(ComponentState.COMPONENT_STATE_EMPTY, isDefaultCancelColor ? ResourceTable.Color_colorActionSheetItemText : ResourceTable.Color_darker_gray)
                        .setOnItemClickListener(mOnItemClickListener)

                        .setBackgroundRadius(25f)
                        .create()
                        .setDimAmount(isBackDim ? 0.6f : 0f)
                        .setAlpha(1f)
                        .show();
                break;
            case ResourceTable.Id_rtv_showWeiXinActionSheet:

                new UIActionSheetDialog.ListWeChatBuilder(this)
                        .addItems(ResourceTable.Strarray_arrays_items_action)
                        .setItemsTextColorResource(ComponentState.COMPONENT_STATE_EMPTY, isDefaultItemColor ? ResourceTable.Color_colorActionSheetWeiXinText : ResourceTable.Color_holo_purple)
                        .setCancelTextColorResource(ComponentState.COMPONENT_STATE_EMPTY, isDefaultCancelColor ? ResourceTable.Color_colorActionSheetWeiXinText : ResourceTable.Color_darker_gray)
                        .setOnItemClickListener(mOnItemClickListener)
//                        .setCancel(ResourceTable.String_cancel)
//                        .setCancelMarginTop(SizeUtil.dp2px(isShowMargin ? 8 : 0))
//                        .setNavigationBarControl(isNavigation ? NavigationBarControlUtil.getNavigationBarControl(isNavigation, isNavigationPlus) : null)
                        .create()
                        .setDimAmount(isBackDim ? 0.6f : 0f)
                        .show();
                break;
            case ResourceTable.Id_rtv_showIconActionSheet:
                UIActionSheetDialog sheetDialog = new UIActionSheetDialog.ListWeChatBuilder(this)
//                        .setBackgroundColor(Color.WHITE.getValue())
                        .addItem("分享微信", ResourceTable.Media_ic_more_operation_share_friend)
                        .addItem("分享朋友圈", ResourceTable.Media_ic_more_operation_share_moment)
                        .addItem("分享微博", ResourceTable.Media_ic_more_operation_share_weibo)
                        .addItem("分享短信", ResourceTable.Media_ic_more_operation_share_chat)
//                        .setNavigationBarControl(isNavigation ? NavigationBarControlUtil.getNavigationBarControl(isNavigation, isNavigationPlus) : null)
                        //设置手指拖拽
                        .setDragEnable(true)
                        .setCancelMarginTop(SizeUtil.dp2px(isShowMargin ? 8 : 0))
                        .setTextDrawablePadding(SizeUtil.dp2px(28))
                        .create()
//                        .setDragEnable(true)
                        .setDimAmount(isBackDim ? 0.6f : 0f);
//                sheetDialog.getListView().setPadding(0, SizeUtil.dp2px(10), 0, SizeUtil.dp2px(10));
                sheetDialog.show();
                break;
            case ResourceTable.Id_rtv_showGridActionSheet:
                if (mDialog == null) {
                    mDialog = new UIActionSheetDialog.GridBuilder(this)
                            .addItem("分享微信", ResourceTable.Media_ic_more_operation_share_friend)
                            .addItem("分享朋友圈", ResourceTable.Media_ic_more_operation_share_moment)
                            .addItem("分享微博", ResourceTable.Media_ic_more_operation_share_weibo)
                            .addItem("分享短信", ResourceTable.Media_ic_more_operation_share_chat)
                            .addItem("保存本地", ResourceTable.Media_ic_more_operation_save)
                            .setItemsTextColorResource(ComponentState.COMPONENT_STATE_EMPTY, isDefaultItemColor ? ResourceTable.Color_colorActionSheetNormalItemText : ResourceTable.Color_holo_green_dark)
                            .setTitle(isShowTitle ? "请选择分享平台" : "")
                            .setCancel(ResourceTable.String_cancel)
                            .setOnShowListener(new DialogInterface.OnShowListener() {
                                @Override
                                public void onShow(CommonDialog dialog) {
//                                    StatusBarUtil.setStatusBarDarkMode(mDialog.getWindow());
                                }

                            })
                            .setCancelMarginTop(SizeUtil.dp2px(isShowMargin ? 8 : 0))
                            .setNumColumns(3)
                            .setItemsTextSize(12)
                            .setItemsImageWidth(SizeUtil.dp2px(60))
                            .setItemsImageHeight(SizeUtil.dp2px(60))
                            .setItemsClickBackgroundEnable(false)
                            .setOnDismissListener(this)
                            .setOnItemClickListener(new UIActionSheetDialog.OnItemClickListener() {
                                @Override
                                public void onClick(BasisDialog dialog, Component itemView, int position) {
                                    if (itemView instanceof Text) {
//                                        Toast.makeText(ActionSheetActivity.this, ((Text) itemView).getText(), Toast.LENGTH_SHORT).show();
//                                        new ToastDialog(getContext()).setText(((Text) itemView).getText()).show();
                                        new ToastDialog(getContext()).setText(((Text) itemView).getText()).show();
                                    }
                                }

                            })
//                            .setNavigationBarControl(isNavigation ? NavigationBarControlUtil.getNavigationBarControl(isNavigation, isNavigationPlus) : null)
                            .create()
                            .setDimAmount(isBackDim ? 0.6f : 0f);
                }

                mDialog.show();
                break;
            case ResourceTable.Id_rtv_showFragmentActionSheet:

                UIActionSheetDialog dialogFragment = new UIActionSheetDialog.ListWeChatBuilder(this)
//                        .setBackgroundColor(Color.WHITE.getValue())
//                        .setTitle(isShowTitle ? "标题" : null)
                        .addItem("分享微信", ResourceTable.Media_ic_more_operation_share_friend)
                        .addItem("分享朋友圈", ResourceTable.Media_ic_more_operation_share_moment)
                        .addItem("分享微博", ResourceTable.Media_ic_more_operation_share_weibo)
                        .addItem("分享短信", ResourceTable.Media_ic_more_operation_share_chat)
//                        .setNavigationBarControl(isNavigation ? NavigationBarControlUtil.getNavigationBarControl(isNavigation, isNavigationPlus) : null)
                        //设置手指拖拽
//                        .setDragEnable(true)
//                        .setCancel(ResourceTable.String_cancel)
//                        .setCancelMarginTop(SizeUtil.dp2px(isShowMargin ? 8 : 0))
                        .setTextDrawablePadding(SizeUtil.dp2px(28))
                        .create()
//                        .setDragEnable(true)
                        .setDimAmount(isBackDim ? 0.6f : 0f);
//                sheetDialog.getListView().setPadding(0, SizeUtil.dp2px(10), 0, SizeUtil.dp2px(10));
                dialogFragment.show();
                break;

            case ResourceTable.Id_sBtn_titleActionSheet:
                switch1.setChecked(!switch1.isChecked());
                break;
            case ResourceTable.Id_sBtn_titleColorActionSheet:
                switch2.setChecked(!switch2.isChecked());
                break;
            case ResourceTable.Id_sBtn_itemColorActionSheet:
                switch3.setChecked(!switch3.isChecked());
                break;
            case ResourceTable.Id_sBtn_cancelColorActionSheet:
                switch4.setChecked(!switch4.isChecked());
                break;
            case ResourceTable.Id_sBtn_marginActionSheet:
                switch5.setChecked(!switch5.isChecked());
                break;
            case ResourceTable.Id_sBtn_backActionSheet:
                switch6.setChecked(!switch6.isChecked());
                break;
            case ResourceTable.Id_sBtn_navigationActionSheet:
                switch7.setChecked(!switch7.isChecked());
                break;
            case ResourceTable.Id_sBtn_navigationPlusActionSheet:
                switch8.setChecked(!switch8.isChecked());
                break;
        }
    }


    @Override
    protected void onBackPressed() {
        super.onBackPressed();
//        if (mDialog != null){
//            mDialog.hide();
//        }
    }

    private UIActionSheetDialog.OnItemClickListener mOnItemClickListener = new UIActionSheetDialog.OnItemClickListener() {
        @Override
        public void onClick(BasisDialog dialog, Component itemView, int position) {
            new ToastDialog(getContext()).setText("item position:" + position).setDuration(1000).show();
        }
    };


    @Override
    public void onDismiss(CommonDialog dialog) {
        mDialog = null;
    }

}
